<?php
!defined('P_W') && exit('Forbidden');
/**
 * 经纪人组服务类
 */
class PW_ExpiredService {
	var $_expiredDB;
	function __construct() {
		$this->_expiredDB = $this->_getExpiredDB();
	}
	/**
	 * 处理过期数据
	 */
	function dealExpireds() {
		$expireds = $this->_initExpireds();
		foreach ($expireds as $dataType=>$value) {
			$dataType = $this->_getDataTypeValue($dataType);
			$houseService = $this->_getHouseSerivce($dataType);//获取房产服务
			foreach ($value as $expiredType=>$v) {
				if (!$v) continue; 
				$expiredType = $this->_getExpiredTypeValue($expiredType);
				$houseService->updateExpired($expiredType,$v);//更新过期信息
			}
		}
	}
	
	/**
	 * 
	 * 获取一条房源过期时间
	 * @param $houseid
	 */
	function getExpire($houseid, $dataType){
		$dataType  = $this->_getDataTypeKey($dataType);
		return $this->_expiredDB->get($houseid, $dataType);
	}
	
	/**
	 * 
	 * 获取一条房源过期时间
	 * @param $houseid
	 */
	function getsExpire($houseids, $dataType, $expiredType){
		$expiredtype = $this->_getExpiredTypeKey($expiredType);
		$dataType  = $this->_getDataTypeKey($dataType);
		return $this->_expiredDB->gets($houseids, $dataType, $expiredtype);
	}	
	
	/**
	 * 添加一条数据
	 * @param $dataType		数据类型
	 * @param $expiredType	过期类型
	 * @param $houseid		房产id
	 * @param $expiredTime	过期时间
	 */
	function addExpired($dataType,$expiredType,$houseid,$expiredTime) {
		$temp = array();
		$temp['datatype'] = $this->_getDataTypeKey($dataType);
		$temp['expiredtype'] = $this->_getExpiredTypeKey($expiredType);
		$temp['houseid'] = (int) $houseid;
		$temp['expiredtime'] = (int) $expiredTime;
		$this->_expiredDB->insert($temp);
	}
	/**
	 * 批量添加数据
	 */
	function addExpireds($expireds) {
		if (!S::isArray($expireds)) return false;
		foreach ($expireds as $value) {
			if (!S::isArray($value)) continue;
			$this->addExpired($value['datatype'],$value['expiredtype'],$value['houseid'],$value['expiredtime']);
		}
	}
	/**
	 * 更新一条数据
	 * @param $dataType		数据类型
	 * @param $expiredType	过期类型
	 * @param $houseid		房产id
	 * @param $expiredTime	过期时间
	 */
	function updateExpires($dataType,$expiredType,$houseid,$expiredTime) {
		$dataType = $this->_getDataTypeKey($dataType);
		$expiredType = $this->_getExpiredTypeKey($expiredType);
		$houseid = (int) $houseid;
		$expiredTime = (int) $expiredTime;
		
		$this->_expiredDB->updateByHouse($dataType,$expiredType,$houseid,$expiredTime);
	}
	/**
	 * 删除一条房产相关的数据
	 * @param $dataType
	 * @param $houseid
	 */
	function deleteHouseExpires($dataType,$expiredType,$houseid) {
		$dataType = $this->_getDataTypeKey($dataType);
		$expiredType = $this->_getExpiredTypeKey($expiredType);
		$this->_expiredDB->deleteHouseExpires($dataType,$expiredType,$houseid);
	}
	function deleteAllHouseExpires($dataType,$houseid) {
		$dataType = $this->_getDataTypeKey($dataType);
		$expiredType = $this->_getExpiredTypeKey($expiredType);
		$this->_expiredDB->deleteAllHouseExpires($dataType,$houseid);
	}
	/**
	 * 删除已过期的数据
	 * @param $ids
	 */
	function deleteExpireds($ids) {
		if (!S::isArray($ids)) return false;
		$this->_expiredDB->deletes($ids);
	}
	/**
	 * 获取房产服务
	 * @param srting $dataType
	 */
	function _getHouseSerivce($dataType) {
		switch ($dataType) {
			case 'sale' : 
				return House::loadService('secondsaleservice');
			case 'hire' : 
				return House::loadService('secondhireservice');
		}
	}
	
	/**
	 * 初始化过期数据
	 */
	function _initExpireds() {
		$expireds = $this->_getExpireds();
		$ids = array_keys($expireds);
		$this->deleteExpireds($ids);//删除已过去数据
		
		$temps = array();
		foreach ($expireds as $key=>$data) {
			$temps[$data['datatype']][$data['expiredtype']][] = $data['houseid'];
		}
		return $temps;
	}
	/**
	 * 获取过期的数据
	 */
	function _getExpireds() {
		return $this->_expiredDB->getExpireds();
	}
	/**
	 * 通过数据库存取的信息获取房产类型
	 * @param $key
	 */
	function _getDataTypeValue($key) {
		$config = $this->_getDataTypeConifg();
		if (!isset($config[$key])) $key = 0;
		return $config[$key];
	}
	/**
	 * 通过类型获取数据库合法的存取值
	 * @param $value
	 */
	function _getDataTypeKey($value) {
		$config = $this->_getDataTypeConifg();
		$config = array_flip($config);
		if (!isset($config[$value])) $value = 'sale';
		return $config[$value];
	}
	/**
	 * 房产类型
	 */
	function _getDataTypeConifg() {
		return array(
			'0'	=> 'sale',
			'1' => 'hire'
		);
	}
	/**
	 * 通过数据库存取的信息获取过期类型
	 * @param $key
	 */
	function _getExpiredTypeValue($key) {
		$config = $this->_getExpiredTypeConifg();
		if (!isset($config[$key])) $key = 0;
		return $config[$key];
	}
	/**
	 * 通过类型获取数据库合法的存取值
	 * @param $value
	 */
	function _getExpiredTypeKey($value) {
		$config = $this->_getExpiredTypeConifg();
		$config = array_flip($config);
		if (!isset($config[$value])) $value = 'expired';
		return $config[$value];
	}
	/**
	 * 过期类型
	 */
	function _getExpiredTypeConifg() {
		return array(
			'0' => 'expired',
			'1' => 'urgent',
			'2' => 'top'
		);
	}
	
	function _getExpiredDB() {
		return House::loadDB('house_expired');
	}
}